<template>
<div  class="loginBJ">
  <div style="width: 600px;background-color: rgba(255,255,255,0.3); margin: 50px auto; padding: 30px 60px;">
    <h1 style="text-align: center; margin: 20px 0;">用户登录</h1>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password" placeholder="请输入密码" type="password"></el-input>
      </el-form-item>
      <el-form-item>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <el-button type="primary"  @click="submitForm('ruleForm')">登录</el-button>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <el-button @click="resetForm()">注册</el-button>
      </el-form-item>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <span><a class="aa" @click="youxiang">邮箱账号登录</a>    |    <a class="aa" @click="shouji">手机号码登录</a></span>
    </el-form>
  </div>
</div>
</template>

<script>

export default {
  data() {
    return {
      ruleForm: {
        username:"",
        password:""
      },
      role:'',
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:8080/users/login';
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.ruleForm);
          console.log(formData)
          this.axios.post(url, formData).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              let jwt = responseBody.data;
              localStorage.setItem('jwt', jwt);
              let url1='http://localhost:8080/users/selectByUserName/'+this.ruleForm.username
              this.axios.get(url1).then((resp)=>{
                let arr=resp.data.data;
                let loginInfo={
                  LoginName:arr.username,
                  openId:"asfafsfsfsdfsdfsdfdsfs",
                  role:arr.roleName
                }
                this.cookie.setCookie(loginInfo,1);
                this.$message({
                  message: '登录成功！',
                  type: 'success'
                });
                this.$router.push('/');
              })

            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm() {
      this.$router.replace('/reg');
    },
    youxiang() {
      // 跳转到登录页面
      this.$router.push('/youxiangLogin');
    },
    shouji(){
      // 跳转到手机号码登录页面
      this.$router.push('/shoujiLogin');
    }
  }
}
</script>

<!--<style scoped>-->
<style>
/*body {*/
/* */
/*}*/
.loginBJ{
  height: 100%;
  background-image: url("/src/assets/loginbj.jpeg");
  background-size: cover;
  overflow: hidden;
}

.aa {
  color: #00A0E9;
}
</style>